package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.customer.persist.Merchant;
import com.checkfree.isolutions.customer.persist.MerchantHomeImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Display an account list for user.
 */
public class CusSelfCareServlet_v1r00 extends EbppxTransactionServlet {

    /**
     * Load vector of accounts into request.
     */
	
	
    public void doTransaction(
        HttpServletRequest req,
        HttpServletResponse res)
    throws ServletException {
        try
        {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

            // get accounts from session and filter out nulls
            final Vector vecAccounts = new Vector();
            boolean boolKenanServerOn = true;
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

            final Vector cfiAccounts = ecsUser.getAccounts();
            final Vector vecBillSubscriptions = new Vector();
            final Vector vecAccountDetails = new Vector();
            String strPhysicalBillSubscription = "";
            String strCategoryDatabase = EbppxCusDBUtil.getCategoryDatabase();
            String cusDatabase = "";
            String strRegisterDate = null;


            final int size = cfiAccounts.size();
            for(int i=0;i<size;i++){
                final Account cfiAccount = (Account) cfiAccounts.elementAt(i);
                final String strAcctExtKey = AcctDetails.getAcctId(cfiAccount.getExternalKey());
                final EbppxBillDeliveryDataBean dataBean = new EbppxBillDeliveryDataBean();
                final AcctDetails acctDetails = new AcctDetails();

                EbppxAcctDetailsDataBean acctDataBean = new EbppxAcctDetailsDataBean();
                acctDataBean = acctDetails.getAcctDetails(strAcctExtKey);
               
                if (cfiAccount!=null){
	            	cusDatabase = EbppxCusDBUtil.getCusDatabase(strCategoryDatabase,strAcctExtKey);
					//Added new line here
					strPhysicalBillSubscription = EbppxCusDBUtil.processCUSPhysicalBillSubscription(strAcctExtKey,cusDatabase,strCategoryDatabase);
					dataBean.setPhysicalBillSubscription(strPhysicalBillSubscription);
					//end added new line

					if(acctDataBean.getEmailAdd()==null){
						//acctDetails.updateEmailAdd(strAcctExtKey,ecsUser.getEmail());
						acctDataBean.setEmailAdd(ecsUser.getEmail());
					}else if(acctDataBean.getEmailAdd().equals("")){
						//acctDetails.updateEmailAdd(strAcctExtKey,ecsUser.getEmail());
						acctDataBean.setEmailAdd(ecsUser.getEmail());
					}
				
					vecAccounts.addElement(cfiAccount);
					vecBillSubscriptions.addElement(dataBean);
					vecAccountDetails.addElement(acctDataBean);
                }
            }

            Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
            User cfiUser = getUserHome().findByLogonIdAndMerchant(ecsUser.getUserId(),cfiMerchant);
            req.setAttribute("LastUpdateDate", cfiUser.getProperty("LastUpdateDate"));
        	
            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, vecAccounts);
            req.setAttribute(EbppxConstants.SELFCARE_ACKNOWLEDGEMENT, "");
            req.setAttribute(EbppxConstants.BILL_SUBSCRIPTIONS, vecBillSubscriptions);
            req.setAttribute(EbppxConstants.ACCT_DETAILS, vecAccountDetails);
            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        }catch (Throwable t){
            throw new ServletException(t.toString(), t);
        }
    }



	/*
	if(!cfiAccount.getProperty("EmailAdd1").equals("blank")){
		strEMailAdd1 = cfiAccount.getProperty("EmailAdd1");
	}
	if(strEMailAdd1.equals("")){
			strEMailAdd1 = ecsu.getEmail();
	}
	if(!cfiAccount.getProperty("EmailAdd2").equals("blank")){
		strEMailAdd2 = cfiAccount.getProperty("EmailAdd2");
	}
	if(strEMailAdd2.equals("")){
		strEMailAdd2 = ecsu.getEmail();
	}

	*/
}